<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('vpt_sa_form.class.php');
require_once('vpt_sa_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_system_attribute_dbc.class.php');


class VPT_SA_Form_Ctrl extends GForm_Ctrl_With_Object {
	protected function create_form() {
		$form = new VPT_SA_Form();

//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new VPT_SA_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		if ($this->form->get_field_value('export_name') && !ereg("^[a-z]{1}[a-z0-9_]{0,254}$", $this->form->get_field_value('export_name'))) {
			$this->form->set_field_error('export_name', 'invalid_export_name');
			$ret = false;
		} else {
			$dbc = $this->get_dbc();
			$object = $this->get_object();

			if ($this->form->get_field_value('export_name') && !TCMS_VPT_System_Attribute_DBC::is_unique_template_plus_export_name($dbc,
																				$object->get_id(),
																				$this->get_permanent_external_param('template'),
																				$this->form->get_field_value('export_name'))) {
				$this->form->set_field_error('export_name', 'duplicate_export_name');
				$ret = false;
			}
		}

		return $ret;
	}


	protected function on_bad_submit() {

//		printbr('lajna');
	}


	protected function create_object() {
		$object = new TCMS_VPT_System_Attribute_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('sa_name', $object->get_sa_name());
		$form->set_field_value('required', $object->get_required());
		$form->set_field_value('export_name', $object->get_sa_export_name());
		$form->set_field_value('default_value', $object->get_default_value());
		$form->set_field_value('export', $object->get_export());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_sa_name($form->get_field_value('sa_name'));
		$object->set_required($form->get_field_value('required'));
		$object->set_sa_export_name($form->get_field_value('export_name'));
		$object->set_default_value($form->get_field_value('default_value'));
		$object->set_export($form->get_field_value('export'));

		if (!$object->get_id()) {
			$object->set_template($this->get_permanent_external_param('template'));
		}
	}
}